package com.feidian.helper.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.net.NetManager;
import com.feidian.helper.ui.view.MyDialog;
import com.feidian.helper.util.DateTimePickDialogUtil;

/**
 * 
 * @author lxq
 * @category /10/3
 * @category 添加求助帖子界面
 * @category 完善服务器功能
 * 
 */
public class AddNewsActivity extends Activity {
	private static final int SEND_HELP_SUCCESS = 0X111;
	private static final int SEND_HELP_FAILURE = 0X112;

	private ImageView mBack;
	private TextView mTitle;

	private EditText mTitleEt;
	private EditText mContentEt;
	private EditText mMonyeEt;
	// private EditText mPhoneEt;
	private TextView mEndTimetET;
	private Button mBn;

	private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"yyyy年MM月dd日   HH:mm:ss");
	private String initStartDateTime;// "2013年9月3日 14:44"; // 初始化开始时间
	
	private SharedPreferences sp = null;

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SEND_HELP_SUCCESS: {
				Toast.makeText(AddNewsActivity.this, "添加求助成功",
						Toast.LENGTH_LONG).show();
				finish();

			}
				break;
			case SEND_HELP_FAILURE: {
				Toast.makeText(AddNewsActivity.this, "添加求助失败",
						Toast.LENGTH_LONG).show();
			}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_add_news);
		String temp = simpleDateFormat.format(new Date()).toString();
		initStartDateTime = temp.substring(0, temp.length() - 3);
		Log.e("simpleDateFormat.format(new Date()).toString()=" + temp,
				".................");
		sp = getSharedPreferences(Table.CommentUserInf,Context.MODE_PRIVATE);
		initView();
	}

	void initView() {

		mBack = (ImageView) findViewById(R.id.back);
		mBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		mTitle = (TextView) findViewById(R.id.title);
		mTitle.setText("我要帮忙");

		mTitleEt = (EditText) findViewById(R.id.my_title);
		mContentEt = (EditText) findViewById(R.id.content);
		mMonyeEt = (EditText) findViewById(R.id.money);
		/*
		 * mPhoneEt=(EditText)findViewById(R.id.phone); if(User.isLogin){
		 * if(User
		 * .phone!=null&&!User.phone.equals("null")&&!User.phone.equals("-1"))
		 * mPhoneEt.setText(User.phone); }
		 */

		mEndTimetET = (TextView) findViewById(R.id.endtime);
		mBn = (Button) findViewById(R.id.ok);
		mEndTimetET.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
						AddNewsActivity.this, initStartDateTime);
				dateTimePicKDialog.dateTimePicKDialog(mEndTimetET);
				Log.e("endTime=", mEndTimetET.getText().toString());

				Log.e("date=", simpleDateFormat.format(new Date()));
			}
		});
		mBn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (mEndTimetET.getText().toString().trim().equals("")) {
					Toast.makeText(AddNewsActivity.this, "截止时间不能为空",
							Toast.LENGTH_LONG).show();
					return;
				}
				if (User.stuId == null || User.password == null|| !sp.getBoolean(Table.isLogin,false)) {
					Toast.makeText(AddNewsActivity.this, "还没登录",
							Toast.LENGTH_LONG).show();
					return;
				}
				if (mContentEt.getText().toString().trim().equals("")) {
					Toast.makeText(AddNewsActivity.this, "内容不能为空",
							Toast.LENGTH_LONG).show();
					return;
				}
				if (mTitleEt.getText().toString().trim().equals("")) {
					Toast.makeText(AddNewsActivity.this, "标题不能为空",
							Toast.LENGTH_LONG).show();
				}
				if (mMonyeEt.getText().toString().trim().equals("")) {
					Toast.makeText(AddNewsActivity.this, "报酬是否为0元",
							Toast.LENGTH_LONG).show();
					return;
				}
				if (mEndTimetET.getText().toString().equals("")) {
					Toast.makeText(AddNewsActivity.this, "截止时间不能为0",
							Toast.LENGTH_LONG).show();
				}
				if (User.phone == null || User.phone.equals("null")
						|| User.phone.equals("-1") || User.qq == null
						|| User.qq.equals("null") || User.qq.equals("-1")) {
					View view = getLayoutInflater().inflate(R.layout.dialog,
							null);
					final MyDialog dialog = new MyDialog(AddNewsActivity.this,
							view, R.style.dialog);
					TextView dialog_title = (TextView) view
							.findViewById(R.id.dialog_title);
					dialog_title.setText("懒人帮提示");
					TextView dialog_content = (TextView) view
							.findViewById(R.id.dialog_content);
					dialog_content.setText("联系方式不足，是否设置?");
					Button dialog_sure = null;
					Button dialog_cancel = null;
					dialog_sure = (Button) view.findViewById(R.id.button1);
					dialog_cancel = (Button) view.findViewById(R.id.button2);
					dialog_sure.setText("是");
					dialog_cancel.setText("否");
					dialog_sure.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							startActivity(new Intent(AddNewsActivity.this,
									PersonalActivity.class));
						}
					});
					dialog_cancel.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View view) {
							dialog.dismiss();
						}
					});
					dialog.show();
				} else {
					/**
					 * 发帖界面接口：Boolean SendHelp（String stuId,String tiitle,String
					 * content ,String startTime,String endTime,String sendStart
					 * ,String accepStuId,String state)
					 */
					String url = "http://115.29.103.105/User/SendHelp";
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair(Table.stuId, User.stuId));
					params.add(new BasicNameValuePair(Table.title, mTitleEt
							.getText().toString()));
					params.add(new BasicNameValuePair(Table.content, mContentEt
							.getText().toString()));
					params.add(new BasicNameValuePair(Table.startTime,
							simpleDateFormat.format(new Date())));
					params.add(new BasicNameValuePair(Table.endTime,
							mEndTimetET.getText().toString()));
					params.add(new BasicNameValuePair(Table.money, mMonyeEt
							.getText().toString()));
					if (User.sendStart != null)
						params.add(new BasicNameValuePair(Table.sendStart,
								User.sendStart));
					else
						params.add(new BasicNameValuePair(Table.sendStart, "5"));

					params.add(new BasicNameValuePair(Table.state, "0"));

					SendHelpThread sendHelpThread = new SendHelpThread(url,
							params);
					sendHelpThread.start();
				}

			}
		});
	}

	class SendHelpThread extends Thread {
		private String mUrl;
		private List<NameValuePair> mParams;

		SendHelpThread(String url, List<NameValuePair> params) {
			mUrl = url;
			mParams = params;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			String content = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			if (content == null) {
				Log.e("post can't return data", ".......");
				Message msg = mHandler.obtainMessage();
				msg.what = SEND_HELP_FAILURE;
				mHandler.sendMessage(msg);
			} else {
				Message msg = mHandler.obtainMessage();
				msg.what = SEND_HELP_SUCCESS;
				mHandler.sendMessage(msg);
			}
			super.run();
		}
	}
}
